package com.fenshi.manager.activity.login;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.fenshi.manager.R;
import com.fenshi.presenter.LoginPresenter;
import com.fenshi.presenter.imp.LoginPresenterImp;
import com.fenshi.view.LoginView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.until.AlertHelper;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements LoginView{

    @InjectView(R.id.login_phone)
    EditText loginPhone;
    @InjectView(R.id.login_pwd)
    EditText loginPwd;
    @InjectView(R.id.login_submit)
    Button loginSubmit;


    LoginPresenter presenter;
    ProgressDialog loading;
//    @InjectView(R.id.login_forget)
//    TextView loginForget;
    @InjectView(R.id.login_remeber)
    CheckBox loginRemeber;

    String username,pwd;

    public void initListener() {
        loginPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                presenter.userNameCheck(loginPhone.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        loginPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                presenter.pwdCheck(loginPwd.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        initActionbar();
        return true;
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_regist;
    }

    @Override
    public void initView() {
        initListener();

        presenter = new LoginPresenterImp(this);
        loading = new AlertHelper(this).LoadingAlert();
        if (username!=null) {
            loginPhone.setText(username);
            loginPwd.setText(pwd);
        }
    }

    @Override
    public void initActionbar() {
        getSupportActionBar().setDisplayShowHomeEnabled(false);
    }

    @Override
    public void initData() {
        SharedPreferences sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
        if (sharedPreferences.getString("username",null)!=null){
            username = sharedPreferences.getString("username",null);
            pwd = sharedPreferences.getString("pwd",null);
        }
    }

    @Override
    public void userNameOrPwdError(String error) {
        loginPhone.setError(error);
    }

    @Override
    public void showAlert(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoading() {
        loading.show();
    }

    @Override
    public void dimissLoading() {
        loading.dismiss();
    }

    @Override
    public void userNameFailed(String error) {
        loginPhone.setError(error);
    }

    @Override
    public void pwdFailed(String error) {
        loginPwd.setError(error);
    }

    @Override
    public boolean getRemeberCheck() {
        return loginRemeber.isChecked();
    }

    @OnClick(R.id.login_submit)
    protected void submit() {
        if (!presenter.userNameCheck(loginPhone.getText().toString())) return;
        if (!presenter.pwdCheck(loginPwd.getText().toString())) return;
        presenter.login(loginPhone.getText().toString(), loginPwd.getText().toString());
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
